package com.ljsw.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

import com.ljsw.activity.R;

/**
 * 超时Dialog
 * 作者: 石锚
 * 版本: 1.0
 * 日期: 2015-10-10
 */
public class TimeOut{
	
	public TimeOut(Context context) {
		super();
		this.context = context;
		WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		display = windowManager.getDefaultDisplay();//获取屏幕分辨率
	}
	private Display display;
	private Context context;
	private TextView txt;
	private Button btnok, btncancle;
	private Dialog dialog;
	private LinearLayout laytimeout;
	
	
	public TimeOut builder(){
		View view = LayoutInflater.from(context).inflate(R.layout.timeout, null);
		laytimeout = (LinearLayout) view.findViewById(R.id.laytimeout);
		txt =  (TextView) view.findViewById(R.id.msgcontent);
		btnok = (Button) view.findViewById(R.id.suere_timeout);
		btncancle = (Button) view.findViewById(R.id.cancel_timeout);
		// 添加Dialog样式
		dialog = new Dialog(context, R.style.TimeOut);
		dialog.setContentView(view);
		dialog.setCanceledOnTouchOutside(false);// 设置点击Dialog外部任意区域不关闭Dialog	
		// 调整dialog背景大小
		laytimeout.setLayoutParams(new FrameLayout.LayoutParams((int) (display
				.getWidth() * 0.55), LayoutParams.WRAP_CONTENT));
		return this;
	}
	
	/**
	 * 设置标题
	 * @param title
	 * @return
	 */
	public TimeOut setTitle(String title){	
		txt.setText(title);
		return this;
	}
	
	/**
	 * 点击事件
	 * @param text
	 * @param listener
	 * @return
	 */
	public TimeOut setQueDing(String text,
			final OnClickListener listener) {
		btnok.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				listener.onClick(v);
				dialog.dismiss();
			}
		});
		return this;
	}
	/**
	 * 点击事件
	 * @param text
	 * @param listener
	 * @return
	 */
	public TimeOut setQueXiao(String text,
			final OnClickListener listener) {
		btncancle.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				listener.onClick(v);
				dialog.dismiss();
			}
		});
		return this;
	}
	public void show() {
		dialog.show();
	}
}
